//
//  GanLuWebLoadPage.m
//  GanLuApp
//
//  Created by luoy on 2024/4/3.
//

#import "GanLuWebLoadPage.h"
#import <Masonry/Masonry.h>
#import <WebKit/WebKit.h>
#import <AudioToolbox/AudioToolbox.h>

@interface GanLuWebLoadPage ()<WKNavigationDelegate, WKScriptMessageHandler>
@property (nonatomic,strong) WKWebView *webView;
@property (nonatomic,strong) UIView *navView;
@property (nonatomic,strong) UILabel *titleLab;
@property (nonatomic,strong) UIButton *backButton;
@end

@implementation GanLuWebLoadPage

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:243.0/255.0 blue:233.0/255.0 alpha:1];
    
//    self.navView = [[UIView alloc] init];
//    self.navView.backgroundColor = UIColor.clearColor;
//    [self.view addSubview:self.navView];
//    [self.navView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
//        make.left.right.equalTo(self.view);
//        make.height.equalTo(@44);
//    }];
    
//    self.titleLab = [UILabel new];
//    self.titleLab.font = [UIFont boldSystemFontOfSize:17];
//    self.titleLab.textColor = UIColor.blackColor;
//    self.titleLab.text = self.titleStr;
//    [self.navView addSubview:self.titleLab];
//    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.center.equalTo(self.navView);
//        make.top.bottom.equalTo(self.navView);
//    }];
//    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    [self.backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.backButton setImage:[UIImage imageNamed:@"Ganlu_ whiteBack"] forState:UIControlStateNormal];
//    [self.navView addSubview:self.backButton];
//    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.bottom.top.equalTo(self.navView);
//        make.width.equalTo(@44);
//    }];
    
    WKPreferences *pre = [[WKPreferences alloc] init];
    pre.javaScriptEnabled = YES;
    pre.javaScriptCanOpenWindowsAutomatically = YES;
    
    WKWebViewConfiguration *con = [[WKWebViewConfiguration alloc] init];
    con.preferences = pre;
    
    WKUserContentController *user = [[WKUserContentController alloc] init];
    con.userContentController = user;
    
    [user addScriptMessageHandler:self name:@"webClose"];
    [user addScriptMessageHandler:self name:@"shock"];
    
    WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:con];
    webView.navigationDelegate = self;
    webView.backgroundColor = UIColor.whiteColor;
    webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    
    self.webView = webView;
    [self.view addSubview:webView];
    
    
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(@0);
    }];
    
    
    
    if (self.url) {
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:self.url]];
        [self.webView loadRequest:request];
    }
    
    
}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setHidden:NO];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setHidden:YES];
}

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
    
    if ([message.name isEqualToString:@"webClose"]) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    if ([message.name isEqualToString:@"shock"]) {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    }

}


- (void)backAction:(id)sender{
    if (self.webView.canGoBack) {
        [self.webView goBack];
        return;
    }
    [self.navigationController popViewControllerAnimated:YES];
}

@end
